import java.util.Stack;

public class BubbSort {

    /*
    * 插排
    * */

    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static void insertSort2(int[] array,int start,int end){
        for (int i = start + 1; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if(array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    /*
    * Hoare法
    * */
    public static void quickSort(int[] array) {
        quick(array,0,array.length - 1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left > right){
            return ;
        }
        if(right - left + 1 == 7){
            insertSort2(array, left, right);
            return;
        }
        int index = findMidNum(array, left, right);
        swap(array,index,left);
        int par = partition3(array,left,right);
        quick(array,left,par - 1);
        quick(array,par + 1,right);
    }

    private static int findMidNum(int[] array,int start,int end){
        int mid = (start + end) / 2;
        if(array[start] < array[end]){
            if(array[mid] < array[start]){
                return start;
            }else if(array[mid] > array[end]){
                return end;
            }else{
                return mid;
            }
        }else{
            if(array[mid] < array[end]){
                return end;
            }else if(array[mid] > array[start]){
                return start;
            }else{
                return mid;
            }
        }
    }

    private static int partition(int[] array, int start, int end) {
        int i = start;
        int pivot = array[start];
        while(start < end){
            while(start < end && array[end] >= pivot){
                end--;
            }
            while(start < end && array[start] <= pivot){
                start++;
            }
            swap(array,start,end);
        }
        swap(array,i,start);
        return start;
    }

    /*
    * 挖坑法
    * */
    private static int partition2(int[] array, int start, int end) {
        int pivot = array[start];
        while(start < end){
            while(start < end && array[end] >= pivot){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= pivot){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = pivot;
        return start;
    }

    /*
    * 前后指针法
    * */

    private static int partition3(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while(cur <= right ){
            if(array[cur] < array[left] && array[prev + 1] != array[cur]){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /*
    * 非递归法
    * */

    public static void quickSort2(int[] array){
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);

        Stack<Integer> stack = new Stack<>();
        if(par > left + 1){
            stack.push(left);
            stack.push(par - 1);
        }
        if(par < right - 1){
            stack.push(par + 1);
            stack.push(right);
        }

        while(!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if(par > left + 1){
                stack.push(left);
                stack.push(par - 1);
            }
            if(par < right - 1){
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

}
